#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  function.py
#  
#  Copyright 2023 dell <dell@DESKTOP-0RFJRKJ>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  
import baseobject as baseob
import re
import pandas as pd
import os
import copy
#读行执行功能函数——读专利 

#父亲结构表
father_list=[]
#儿子结构表
son_list=[]
   
def get_thesis(data,num):
    list_data = []
    for temp in data.split(" "):
        if temp != "":
            list_data.append(re.sub("\n", "", temp))
    return list_data[num]#读行函数
#读行执行功能函数——读论文信息    
def get_thesis_inf(data,num):
    #print("开始读行")
    list_data = []
    temp_tt=""
    tailabel=0
    if valid_ETAL_VOL_PAGE(data)==2:
      return data
    elif valid_ETAL_VOL_PAGE(data)==0:
      return ""
    else:
     for temp in data.split(","):
        #print(temp)
        if temp=="":
         continue
        elif temp != "" and valid_page(temp)==0:
            temp_tt+=temp+","
        elif valid_page(temp)==1:               
             temp_tt+=temp
             list_data.append(re.sub("\n", "", temp_tt))
             #print(temp_tt)
             temp_tt=""
    return list_data[num]#读行函数
#获取data目录下所有文件名
def read_directory():#获取data目录下所有文件名
    path="./data/"
    files=os.listdir(path)
    positions=[]
    i=0
    for file in files:
        position=path+"\\"+file
        positions.append(position)#读列表  
    return positions
#查找同名父亲，填入儿子    
def search_add_father(father_name,son):
    for father in father_list:
        if father.father_name==father_name:
            father.add(son)
#查找是否有这个父亲            
def check_father(father_list,father_name):
     
     for father in father_list:
         if father_name==father.father_name:
             return father
     return None
#查找是否有这个儿子        
def check_son(son_list,son_name):
     
     for son in son_list:
         if son_name==son.son:
             return son    
     return None

def calculate_data(name):#单个统计CP接口
    f = open(name)#打开分类文档
    datas = f.readlines()
    FLAG = 0
    list_thesis = []
    fatherflag=""
    list_thesis_tt = []
    dic_thesis_tt = {}
    #匹配CP字段函数
    for data in datas:
        flag_type = data[:2]
        if flag_type == "CP":#格式匹配CP
            FLAG = 1
            thesis = get_thesis(data,1)
            if(check_father(father_list,thesis)==None):
                   father=baseob.father_article(thesis)#载入新的父亲
                   father_list.append(father)                   
      
            else:
                    father=check_father(father_list,thesis)#载入检索到的父亲                
        elif flag_type == "  " and FLAG ==1:#前两格为空，并且是CP之下，鉴定为可能被引或是PN中专利号
            flag_type2=data[:6]
            if flag_type2!="      " and FLAG==1:#前六格不为空，鉴定为可能是施引
                thesis = get_thesis(data,0)
                if(check_father(father_list,thesis)==None):#发现表中没有同名父亲
                   father_list.append(baseob.father_article(thesis))
                   father=baseob.father_article(thesis)#载入新的父亲
                else:
                    father=check_father(father_list,thesis)#否则载入检索到的父亲                      
            elif flag_type2=="      " and FLAG==1:# 前六格为空可能是被引
             thesis = get_thesis(data,0)            
             if check_son(son_list,thesis)==None:
                 son_temp=baseob.son_article(thesis)#创造子类
                 son_list.append(son_temp)#有则不加，否则加入表
                 if check_son(father.sons,thesis)==None:
                  father.add(son_temp)#父亲中加入下属材料

             else:
                 if check_son(father.sons,thesis)==None:
                  search_add_father(father.father_name,check_son(son_list,thesis)) 

             list_thesis.append(thesis)
             list_thesis_tt.append(thesis)
        else:
            FLAG = 0
            #print("/n"+"一轮结束了，现在的父亲是"+father.father_name)
            #show_son(father)
            if len(list_thesis_tt) > 0:
                list_thesis_tt = set(list_thesis_tt)
                for tt in list_thesis_tt:
                    if tt not in dic_thesis_tt:#推入不重复的专利号，统计record
                        dic_thesis_tt[tt] =1
                    else:
                        dic_thesis_tt[tt] += 1
            list_thesis_tt = []

    dic_freq = {}#统计词频
    for the in list_thesis:
        if the not in dic_freq:
            dic_freq[the] = 1
        else:
            dic_freq[the] += 1
    
    return [dic_freq,dic_thesis_tt]#只传输字典
#格式验证，把形如etal-----vol-----page的一段字符串识别并提取出来，作为论文信息使用
def valid_ETAL_VOL_PAGE(data):
    etalinf=['ET AL','et al']
    eta_label=0
    volinf=[' vol.',' VOlUME',' VOL.']
    vol_label=0
    pageinf=[' pa',' pp',' p.',' PAGE']
    page_label=0
    for eta in etalinf: 
     if data.find(eta)!=-1:
        eta_label=1#有et al信息
        break
    
    if eta_label==0:#发现没有et al
     for vol in volinf: 
      if data.find(vol)!=-1:
        vol_label=1#有vol信息
        break
    
    if eta_label==0 and vol_label==0:#没有vol信息和ET AL信息，不提出
        return 0
    else:
     for page in pageinf: #检测page信息
      if data.find(page)!=-1:
        page_label=1#有page信息
        break
     if page_label==1:
        return 1#有etal或者vol且有page
     elif page_label==0: 
         return 2 #有etal或者vol但是没有page
#验证是否有页码信息
def valid_page(data):
    pageinf=[' pa',' pp',' p.',' PAGE']
    page_label=0
    for page in pageinf:
     if data.find(page)!=-1:
      page_label=1
      break
    return page_label#该分段具有页号，在此断开
    
def calculate_data_inf(name):#单个统计CR接口
    f = open(name)#打开分类文档
    datas = f.readlines()
    FLAG = 0
    list_thesis = []
    list_thesis_tt = []
    dic_thesis_tt = {}
    for data in datas:
        flag_type = data[:2]
        
        if flag_type == "CR":#格式匹配CR
            FLAG = 1
        elif flag_type == "  " and FLAG ==1:#
            flag_type2=data[:6]
            if flag_type2=="      " and FLAG==1:
             thesis = get_thesis_inf(data[6:],0)
            if thesis!="":
              list_thesis.append(thesis)
              list_thesis_tt.append(thesis)           
        else:
            FLAG = 0
            if len(list_thesis_tt) > 0:
                list_thesis_tt = set(list_thesis_tt)
                for tt in list_thesis_tt:
                    if tt not in dic_thesis_tt:#推入不重复的论文信息，统计record
                        dic_thesis_tt[tt] =1
                    else:
                        dic_thesis_tt[tt] += 1
            list_thesis_tt = []
    #print(dic_thesis_tt)          
    dic_freq = {}#统计词频
    for the in list_thesis:
        if the not in dic_freq:
            dic_freq[the] = 1
        else:
            dic_freq[the] += 1
    return [dic_freq,dic_thesis_tt]#只传输字典
def calculate_data_pn(name):#单个统计PN接口
    f = open(name)#打开分类文档
    datas = f.readlines()
    FLAG = 0
    pn_list=[]
    for data in datas:
        flag_type = data[:2]        
        if flag_type == "PN":#格式匹配PN
            nums=1
            pn_datas=data[3:]
            #print(pn_datas)
            for pn_data in pn_datas:
                if pn_data==";":
                   nums+=1 #统计个数
            pn=baseob.PN_list(pn_datas,nums)
            pn_list.append(pn)          
         
    return pn_list#只传输pn表 
     
def multicalculate_data():
    positions=read_directory()
    #cp信息的数据帧
    df_father={}
    df_father_tt={}
    #cr信息的数据帧
    df_father_inf={}
    df_father_inf_tt={}
    document_number=1
    pn_information=[]
    for position in positions:
        print("目前处理到第"+str(document_number)+"个文档，文档路径为:"+position)
        couple= calculate_data(position)
        couple_inf=calculate_data_inf(position)
        pn_list=calculate_data_pn(position)
        #统计CP内行的fre和rec
        for k,v in couple[0].items():#统计CP频次
            if(k not in df_father):
               df_father[k]=v
                #
            else: 
                df_father[k]+=v
        for k,v in couple[1].items():#统计CPrecoed
            if(k not in df_father_tt):
               df_father_tt[k]=v
                
            else: 
                df_father_tt[k]+=v 
         
        for k,v in couple_inf[0].items():#统计CR频次
            if(k not in df_father_inf):
               df_father_inf[k]=v
                
            else: 
                df_father_inf[k]+=v
        for k,v in couple_inf[1].items():#统计CRrecoed
            if(k not in df_father_inf_tt):
               df_father_inf_tt[k]=v             
            else: 
                df_father_inf_tt[k]+=v
        for pn in  pn_list:
            pn_information.append(pn)                     
    for son in son_list:
        son.set_record(df_father_tt[son.son])
        son.set_frequency(df_father[son.son])                              
    #输出CP为excel                   
    output_csv(df_father,df_father_tt,0)
    output_csv(df_father_inf,df_father_inf_tt,1)
    output_pn(pn_information)    
        
#按分类输出为csv函数
def output_csv(df_father,df_father_tt,num):
   #输出CP
   if num==0:
    dic_tocsv = {"被引专利":[],"frequency":[]}
    for k,v in df_father.items():
        dic_tocsv["被引专利"].append(k)
        dic_tocsv["frequency"].append(v)
    df_csv = pd.DataFrame(dic_tocsv)
 
    dic_tocsv_tt = {"被引专利":[],"record":[]}
    for k,v in df_father_tt.items():
        dic_tocsv_tt["被引专利"].append(k)
        dic_tocsv_tt["record"].append(v)
    df_csv_tt = pd.DataFrame(dic_tocsv_tt)

    df_cr_merge=df_csv.merge(df_csv_tt,how='inner',on='被引专利')
    df_cr_merge.to_csv("专利引用导出结果融合.csv", index=False, sep=',', encoding="gb18030")
    
    dic_tocsv_network={"施引专利(PN)":[],"被引专利(cp)":[],"被引record":[],"被引frequency":[]}
    
    for father in father_list:         
        for son in father.sons:
            #print(father.father_name+"子："+son.son)
            dic_tocsv_network["施引专利(PN)"].append(father.father_name)
            dic_tocsv_network["被引专利(cp)"].append(son.son)
            dic_tocsv_network["被引record"].append(son.record)
            dic_tocsv_network["被引frequency"].append(son.frequency)
    df_csv_network=pd.DataFrame(dic_tocsv_network)
    df_csv_network.to_csv("论文有向网络简单结构.csv", index=False, sep=',', encoding="gb18030")
    
   #输出CR
   if num==1:
    dic_tocsv = {"被引论文信息":[],"frequency":[]}
    for k,v in df_father.items():
        dic_tocsv["被引论文信息"].append(k)
        dic_tocsv["frequency"].append(v)
    df_csv = pd.DataFrame(dic_tocsv)
    #df_csv=df_csv.sort_values('frequency',ascending=[False])
    #df_csv.to_csv("论文信息导出结果——按专利分布.csv", index=False, sep=',', encoding="gb18030")
    
    dic_tocsv_tt = {"被引论文信息":[],"record":[]}
    for k,v in df_father_tt.items():
        dic_tocsv_tt["被引论文信息"].append(k)
        dic_tocsv_tt["record"].append(v)
    df_csv_tt = pd.DataFrame(dic_tocsv_tt)
    
    #df_csv_tt=df_csv_tt.sort_values('record',ascending=[False])
    #df_csv_tt.to_csv("论文信息导出结果——按家族分布.csv", index=False, sep=',', encoding="gb18030")    
    df_cr_merge=df_csv.merge(df_csv_tt,how='inner',on='被引论文信息')
    df_cr_merge.to_csv("论文信息导出结果融合.csv", index=False, sep=',', encoding="gb18030") 
    
def output_pn(pn_information):#输出pn为csv
    dic_tocsv={"PN内专利":[],"总数":[]}
    for pn in pn_information:
        dic_tocsv["PN内专利"].append(pn.pn_datas)
        dic_tocsv["总数"].append(pn.nums)
    df_csv = pd.DataFrame(dic_tocsv)
    df_csv.to_csv("PN信息导出.csv", index=False, sep=',', encoding="gb18030")     
